package org.jeecg.modules.networking.scheduled;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.carLedger.service.ICarLedgerService;
import org.jeecg.modules.excessiveIssued.entity.ExcessiveIssued;
import org.jeecg.modules.excessiveIssued.service.IExcessiveIssuedService;
import org.jeecg.modules.measuresIssued.entity.MeasuresIssued;
import org.jeecg.modules.measuresIssued.service.IMeasuresIssuedService;
import org.jeecg.modules.networking.service.INetWorkingService;
import org.jeecg.modules.violationIssued.entity.ViolationIssued;
import org.jeecg.modules.violationIssued.service.IViolationIssuedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
@Slf4j
public class MyScheduled {

    @Autowired
    private INetWorkingService netWorkingService;
    @Autowired
    private IMeasuresIssuedService measuresIssuedService;//移动源管控措施
    @Autowired
    private IViolationIssuedService violationIssuedService;//违规通行车辆名单
    @Autowired
    private IExcessiveIssuedService excessiveIssuedService;//排放超标车辆名单

    @Autowired
    private ICarLedgerService carLedgerService;//电子台账

    @Scheduled(cron = "0 * * * * ?")//朝九晚五的每小时执行一次
    public void configureTasks() throws Exception{
        JSONObject jsonObject = netWorkingService.syncInfo();
        log.info("心跳结果: {}", jsonObject);
        Integer code = jsonObject.getInteger("code");
        if(code == 200){
            String syncCode = jsonObject.getString("syncCode");
            switch (syncCode){
                case "B01"://有最新违规车辆名单待企业更新
                    JSONObject breakPassRules = netWorkingService.breakPassRules();
                    log.info("违规车辆名单返回结果: {}", breakPassRules);
                    JSONArray breakPassRulesJSONArray = breakPassRules.getJSONArray("value");
                    List<ViolationIssued> violationIssuedList = breakPassRulesJSONArray.toJavaList(ViolationIssued.class);
                    violationIssuedService.saveBatch(violationIssuedList);
                    break;
                case "B02"://有最新黑名单车辆名单待企业更新
                    JSONObject breakEnvRules = netWorkingService.breakEnvRules();
                    log.info("黑名单车辆名单返回结果: {}", breakEnvRules);
                    JSONArray breakEnvRulesJSONArray = breakEnvRules.getJSONArray("value");
                    List<ExcessiveIssued> excessiveIssued = breakEnvRulesJSONArray.toJavaList(ExcessiveIssued.class);
                    excessiveIssuedService.saveBatch(excessiveIssued);
                    break;
                case "C01"://有最新移动源管控措施待企业更新
                    JSONObject controlStrategy = netWorkingService.getControlStrategy();
                    log.info("移动源管控措施返回结果: {}", controlStrategy);
                    JSONObject value = controlStrategy.getJSONObject("value");
                    MeasuresIssued measuresIssued = value.toJavaObject(MeasuresIssued.class);
                    measuresIssuedService.save(measuresIssued);
                    break;
                case "S01"://无最新信息更新
            }
        } else if (code == 401){
            netWorkingService.updateLocalTokenInfo();
            configureTasks();
        }
    }

    @Scheduled(cron = "0 33 0 * * ?")//每天0点13分执行一次
    public void scheduledTasks() throws Exception{
        int vehiclePassRecordsNum = carLedgerService.getVehiclePassRecordsNum();
        Calendar calendar = Calendar.getInstance();// 获取当前时间
        calendar.add(Calendar.DAY_OF_MONTH, -1);// 将时间减去一天
        Date yesterday = calendar.getTime();// 获取昨天的日期
        JSONObject jsonObject = netWorkingService.putVehiclePassRecordsNum(yesterday, vehiclePassRecordsNum);
        log.info("每日上报数据返回结果: {}", jsonObject);
    }

}
